/**
 * <p>Copyright (c) 2010 by DRM. </p>
 */

package com.responsemeasure.util;

import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import com.responsemeasure.R;
import com.responsemeasure.DRMApplication;
import com.responsemeasure.to.NineOfFourReportTO;

/**
 * <p>
 * <b>CLASS:</b> com.android.drm.util.ChartHelp
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>DESCRIPTION:</b><br/>
 * The helper class of charting.
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>MODIFICATION HISTORY:</b>
 * <p>
 * [2010/07/29] e2005g: Initial version.
 * [2010/08/25] michellem: Added methods for bar chart for correct hits.
 * [2010/09/19] michellem: Brought chart wordings from code to strings.xml.
 * [2010/09/30] ericaker: changed how average is computed for bar chart
 * [2010/10/05] ericaker: changed how average is computed for line chart
 * </p>
 * </p>
 * 
 * ...........................................................................
 * 
 * @author e2005g
 * @since 1.0
 */

public class ChartHelper {

	public static XYMultipleSeriesRenderer getXYChartRenderer(Context context) {//LINE CHART
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(Color.BLUE);
		r.setPointStyle(PointStyle.SQUARE);
		r.setFillBelowLine(true);
		r.setFillBelowLineColor(Color.WHITE);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		r = new XYSeriesRenderer();
		r.setPointStyle(PointStyle.CIRCLE);
		r.setColor(Color.MAGENTA);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK); 
		renderer.setLabelsColor(Color.BLACK);
		renderer.setChartTitle(context.getString(R.string.fourOfNine_chart_title));
		renderer.setXTitle(context.getString(R.string.response_bar_chart_xTitle));
		renderer.setYTitle(context.getString(R.string.response_bar_chart_yTitle));
		setXYChartSettings(renderer, context);
		return renderer;
	}

	private static void setXYChartSettings(XYMultipleSeriesRenderer renderer, Context context) {//LINE CHART
		renderer.setChartTitle(context.getString(R.string.fourOfNine_chart_title));
		renderer.setXTitle(context.getString(R.string.response_bar_chart_xTitle));
		renderer.setYTitle(context.getString(R.string.response_bar_chart_yTitle));
		renderer.setXAxisMin(0.0);
		renderer.setXAxisMax(100.5);//x axis max
		renderer.setYAxisMin(0.0);
		renderer.setYAxisMax(10000); // = 10 seconds
		//renderer.setChartValuesTextSize(20);
		//renderer.setAxisTitleTextSize(12);
		renderer.setXLabels(6);
		//renderer.setDisplayChartValues(true);
		//renderer.setTextTypeface("Verdana", 30);
		//renderer.setTextTypeface("sans_serif", Typeface.BOLD);
		//renderer.setTextTypeface("monospace", Typeface.NORMAL);
		//renderer.setLabelsTextSize(12);
		renderer.setShowGrid(true);
		// set to 10 seconds by eric. Ymax needs some attention
		// max Y total time is 10seconds 
	}	

	public static XYMultipleSeriesDataset getFourOfNineXYDataset(Context context,
			DRMApplication drmApplication, Bundle bundle) {//-------------------------LINE CHART
		
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getAverageIntervals(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		// List<Double> averageList = reportTo.getAverages(); // compute average based on total. Do NOT get
		List<Long> totalList = reportTo.getTotals();

		//XYSeries series = new XYSeries(bundle.getString("averageTitle"));
		XYSeries series = new XYSeries(context.getString(R.string.avg_response_bar_chart_legend));
		// compute a start call it timeIndex
		int timeIndex =0;
		double TotalTime = 0.0;
		timeIndex=reportTo.getLength()-100; // only do last 100 points
	    if(timeIndex<0) timeIndex =0;
	    
		// the first data point needs to use the prev 10 points and the next 10, so we need 20 total datapoints
		// special case of less than 10 points
			for (; timeIndex < reportTo.getLength(); timeIndex ++){
				if (timeIndex<10){ // first 10 points are simple
					// compute one average
					for(int j=0;j<timeIndex+1;j++){
						TotalTime=TotalTime+totalList.get(j);
					}
					series.add(timeIndex,TotalTime/(timeIndex+1));
					TotalTime=0.0;
				} // end if <10
				if(timeIndex>=10){
					 for(int j=timeIndex-9;j<timeIndex+1;j++){
					   TotalTime=TotalTime + totalList.get(j);
					 }
					 TotalTime = TotalTime/10.0; // do this on separate line so it is easier to look at with debugger
					 series.add(timeIndex,TotalTime);
					 TotalTime=0.0;
				} // end if >=10
	         } // end timeIndex loop
		
		
		
		dataset.addSeries(series);

		//series = new XYSeries(bundle.getString("totalTitle"));
		series = new XYSeries(context.getString(R.string.total_response_bar_chart_legend));
		timeIndex=reportTo.getLength()-100; // only do last 100 points
	    if(timeIndex<0) timeIndex =0;  // there are less than 100 so start at zero
	    
		for (; timeIndex < reportTo.getLength(); timeIndex ++){
			series.add(timeIndex, totalList.get(timeIndex));
		  } // end for loop
		
		dataset.addSeries(series);

		return dataset;
	}

	public static XYMultipleSeriesDataset getFourOfNineBarDataset(Context context,
			DRMApplication drmApplication, Bundle bundle) {//-------------------------BAR CHART
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getAverageIntervals(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		//List<Double> averageList = reportTo.getAverages();
		List<Long> totalList = reportTo.getTotals();
		Double TotalTime=0.0;
		
		CategorySeries series = new CategorySeries(context.getString(R.string.avg_response_bar_chart_legend));
		
		// compute a start call it timeIndex
		int timeIndex =0;
		timeIndex=reportTo.getLength()-10;
	    if(timeIndex<0) timeIndex =0;
	    
		// the first data point needs to use the prev 10 points, so we need 20 total datapoints
		// special case of less than 20 points
			for (; timeIndex < reportTo.getLength(); timeIndex ++){
				if (timeIndex<10){ // first 10 points are simple
					// compute one average
					for(int j=0;j<timeIndex+1;j++){
						TotalTime=TotalTime+totalList.get(j);
					}
					series.add(TotalTime/(timeIndex+1));
					TotalTime=0.0;
				} // end if <10
				if(timeIndex>=10){
					 for(int j=timeIndex-9;j<timeIndex+1;j++){
					   TotalTime=TotalTime + totalList.get(j);
					 }
					 TotalTime = TotalTime/10.0;
					 series.add(TotalTime);
					 TotalTime=0.0;
				} // end if >10
			} // end timeIndex loop
			
	
		dataset.addSeries(series.toXYSeries());

		series = new CategorySeries(context.getString(R.string.total_response_bar_chart_legend));
		
		timeIndex=reportTo.getLength()-10;
	    if(timeIndex<0) timeIndex =0;
	    
	    for (; timeIndex < reportTo.getLength(); timeIndex++) {
				series.add(totalList.get(timeIndex) );
			}
		dataset.addSeries(series.toXYSeries());

		return dataset;
	}	

	public static XYMultipleSeriesRenderer getBarRenderer(Context context) {//BAR CHART
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setColor(Color.BLUE);
		renderer.addSeriesRenderer(r);
		r = new SimpleSeriesRenderer();
		r.setColor(Color.MAGENTA);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK);
		renderer.setLabelsColor(Color.BLACK);
		setChartSettings(renderer, context);
		return renderer;
	}
	
	private static void setChartSettings(XYMultipleSeriesRenderer renderer, Context context) {//BAR CHART
		renderer.setChartTitle(context.getString(R.string.fourOfNine_chart_title));
		renderer.setXTitle(context.getString(R.string.response_bar_chart_xTitle));
		renderer.setYTitle(context.getString(R.string.response_bar_chart_yTitle));
		renderer.setXAxisMin(0.5);
		//renderer.setXAxisMax(10.5);//x axis max
		renderer.setXAxisMax(10.5);//x axis max
		renderer.setYAxisMin(0.0);
		renderer.setYAxisMax(10000);
		// set to 10 seconds by eric. Ymax needs some attention
		// max Y total time is 10seconds 
	}
	
	public static XYMultipleSeriesDataset getFourOfNineHitBarDataset(Context context,
			DRMApplication drmApplication, Bundle bundle) {//-------------------------HIT BAR CHART
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		NineOfFourReportTO reportTo = drmApplication.getDataHelper()
				.getHitCounts(bundle.getLong("userId"),
						bundle.getLong("actionId"),
						bundle.getString("userName"),
						bundle.getString("actionName"));
		List<Integer> correctHitList = reportTo.getCorrectHits();

		CategorySeries series = new CategorySeries(context.getString(R.string.hit_bar_chart_legend));
		if (reportTo.getLength() <= 10){//if less than or equal to 10 show all
		
			for (int i = 0; i < reportTo.getLength(); i++) {
				series.add(correctHitList.get(i));
			}
		}else{//else show last 10
		
			for (int i = reportTo.getLength() - 10; i < reportTo.getLength(); i++) {
				series.add(correctHitList.get(i));
			}
		}
		dataset.addSeries(series.toXYSeries());

		return dataset;
	}	
	
	public static XYMultipleSeriesRenderer getHitBarRenderer(Context context) {//HIT BAR CHART
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setColor(Color.BLUE);
		renderer.addSeriesRenderer(r);
		renderer.setAxesColor(Color.BLACK);
		renderer.setLabelsColor(Color.BLACK);
		setHitChartSettings(renderer, context);

		return renderer;
	}

	private static void setHitChartSettings(XYMultipleSeriesRenderer renderer, Context context) {//HIT BAR CHART
		renderer.setChartTitle(context.getString(R.string.fourOfNine_chart_title));
		renderer.setXTitle(context.getString(R.string.hit_bar_chart_xTitle));
		renderer.setYTitle(context.getString(R.string.hit_bar_chart_yTitle));		
		renderer.setXAxisMin(0.5);
		renderer.setXAxisMax(10.5);//x axis max
		renderer.setYAxisMin(0);
		renderer.setYAxisMax(5);
	}
	
}
